import {
  CoolController,
  BaseController,
  TagTypes,
  CoolUrlTag,
} from '@cool-midway/core';
import { GoodsPromoMtEntity } from '../../entity/promo_mt';
import { GoodsPromoMtService } from '../../service/promo_mt.service';
import { Body, Inject, Post } from '@midwayjs/core';
import { GoodsPromoOrderService } from '../../service/promo_order.service';

/**
 * 促销汇总前端
 */
@CoolUrlTag({
  key: TagTypes.IGNORE_TOKEN,
  value: ['page', 'info'],
})
@CoolController({
  api: ['page', 'info'],
  entity: GoodsPromoMtEntity,
  service: GoodsPromoMtService,
  pageQueryOp: {
    keyWordLikeFields: ['a.promName'],
    fieldEq: ['a.promType'],
    addOrderBy: {
      createTime: 'desc',
    },
    where: () => {
      const now = new Date();
      return [
        ['a.status = :status', { status: 1 }],
        ['a.startTime <= :now', { now }],
        ['a.endTime >= :now', { now }],
      ];
    },
  },
})
export class AppGoodsPromoMtController extends BaseController {
  @Inject()
  goodsPromoMtService: GoodsPromoMtService;

  @Inject()
  goodsPromoOrderService: GoodsPromoOrderService;

  @Inject()
  ctx;

  @Post('/getActivePromos')
  async getActivePromos() {
    return this.ok(await this.goodsPromoMtService.getActivePromos());
  }

  @Post('/getByPromType')
  async getByPromType(@Body() params: { promType?: string }) {
    return this.ok(
      await this.goodsPromoMtService.getByPromType(params.promType, 1)
    );
  }

  @Post('/getComboDetailsByType')
  async getComboDetailsByType(@Body() params: { promType?: string }) {
    return this.ok(
      await this.goodsPromoMtService.getComboDetailsByType(params.promType)
    );
  }

  @Post('/getCombosByGoodsId')
  async getCombosByGoodsId(@Body() params: { goodsId: string }) {
    return this.ok(
      await this.goodsPromoMtService.getCombosByGoodsId(params.goodsId)
    );
  }

  /**
   * 创建套餐订单
   */
  @Post('/createComboOrder')
  async createComboOrder(@Body() params: { billNo: string; remark?: string }) {
    const userId = this.ctx.user.erpBusinessId;
    return this.ok(
      await this.goodsPromoOrderService.createComboOrder(
        userId,
        params.billNo,
        params.remark
      )
    );
  }

  /**
   * 创建秒杀订单
   */
  @Post('/createSeckillOrder')
  async createSeckillOrder(
    @Body()
    params: {
      billNo: string;
      goodsId: string;
      quantity: number;
      remark?: string;
    }
  ) {
    const userId = this.ctx.user.erpBusinessId;
    return this.ok(
      await this.goodsPromoOrderService.createSeckillOrder(
        userId,
        params.billNo,
        params.goodsId,
        params.quantity,
        params.remark
      )
    );
  }

  /**
   * 创建买赠订单
   */
  @Post('/createBuyGiftOrder')
  async createBuyGiftOrder(
    @Body()
    params: {
      billNo: string;
      goodsId: string;
      quantity: number;
      remark?: string;
    }
  ) {
    const userId = this.ctx.user.erpBusinessId;
    return this.ok(
      await this.goodsPromoOrderService.createBuyGiftOrder(
        userId,
        params.billNo,
        params.goodsId,
        params.quantity,
        params.remark
      )
    );
  }
}
